package com.abstractgames;

import org.lwjgl.input.Mouse;

//import com.badlogic.gdx.Application.ApplicationType;
//import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;

public class Input implements InputProcessor {
	public static int button = 0;
	public static int scroll =0;
	
	public static final int RIGHT = 0;
	public static final int LEFT = 1;
	public static final int UP = 2;
	public static final int DOWN = 3;
	public static final int SIT = 4; 
	
	public static boolean DRAG = false;
	public static int DRAGx;
	public static int DRAGy;
	
	public static int X;
	public static int Y;
	
	int rex(int x){
		return (int)((x- Screen.CAMWIDTH/2)*Screen.camera.zoom + Screen.camera.position.x );
	}
	
	int rey(int y){
		return (int)((Screen.HEIGHT-y- Screen.CAMHEIGHT/2)*Screen.camera.zoom + Screen.camera.position.y );
	}
	
	void draw(int x, int y){
		x = rex(x);
		y = rey(y);
		X = x; Y = y;
		if(Mouse.isInsideWindow()) {
			int i =(int)(Math.floor(x/32));
			int j =(int)(Math.floor(y/32));
			int n = j+Physics.MAXY*i;
			if((n < Physics.cells.size())&&(i > -1)&& (j > -1))
				if(Physics.cells.get(n).isEmpty()){
					Atom atom = new Atom(i,j,Input.button);
					Physics.atoms.add(atom);
					Physics.cells.get(j+Physics.MAXY*i).getAtom(Physics.atoms.size()-1);
				}

		}
	}
	
	void drag(int x, int y){

		float z = Screen.camera.zoom;
		Screen.camera.translate((-x+DRAGx)*z,(y-DRAGy)*z,0);
		DRAGx = x; 
		DRAGy = y;

	}
	
	@Override
	public boolean keyDown(int keycode) {
				
		if (keycode == Keys.DPAD_UP) button = UP;
		if (keycode == Keys.DPAD_LEFT) button = LEFT;
		if (keycode == Keys.DPAD_DOWN) button = DOWN;
		if (keycode == Keys.DPAD_RIGHT) button = RIGHT;
		if (keycode == Keys.ENTER) button = SIT;
		if (keycode == Keys.SPACE) Game.running = !Game.running;
		if (keycode == Keys.SHIFT_LEFT || keycode == Keys.SHIFT_RIGHT) DRAG = true;
		if (keycode == Keys.ESCAPE) { Physics.atoms.clear(); for(int i = 0; i < Physics.cells.size(); i++) Physics.cells.get(i).empty();}
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		if (keycode == Keys.SHIFT_LEFT || keycode == Keys.SHIFT_RIGHT) DRAG = false;
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		scroll+=amount;
		Screen.camera.zoom=(float)Math.exp(scroll/(2*Math.E));
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		if (DRAG) {DRAGx = x; DRAGy = y;}
		else draw(x,y);
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		if (DRAG) drag(x, y);
		else draw(x,y);
		return false;
	}

	@Override
	public boolean touchMoved(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}
//	public static final int UP = 0;
//	public static final int DOWN = 1;
//	public static final int LEFT = 2;
//	public static final int RIGHT = 3;
//
//	public static final int JUMP = 4;
//	public static final int SHOOT = 5;
//
//	public static final int ESCAPE = 6;
//
//	public boolean[] buttons = new boolean[64];
//	public boolean[] oldButtons = new boolean[64];
//
//	public void set (int key, boolean down) {
//		int button = -1;
//
//		if (key == Keys.DPAD_UP) button = UP;
//		if (key == Keys.DPAD_LEFT) button = LEFT;
//		if (key == Keys.DPAD_DOWN) button = DOWN;
//		if (key == Keys.DPAD_RIGHT) button = RIGHT;
//
//		if (key == Keys.Y) button = JUMP;
//		if (key == Keys.Z) button = JUMP;
//		if (key == Keys.X) button = SHOOT;
//		if (key == Keys.C) button = JUMP;
//		if (key == Keys.A) button = JUMP;
//		if (key == Keys.S) button = SHOOT;
//		if (key == Keys.D) button = JUMP;
//
//		if (key == Keys.ESCAPE || key == Keys.MENU) button = ESCAPE;
//
//		if (button >= 0) {
//			buttons[button] = down;
//		}
//	}
//
//	public void tick () {
//		for (int i = 0; i < buttons.length; i++) {
//			oldButtons[i] = buttons[i];
//		}
//
//		if (Gdx.app.getType() == ApplicationType.Android) {
//			boolean left = false;
//			boolean right = false;
//			boolean z = false;
//			boolean s = false;
//
//			for (int i = 0; i < 2; i++) {
//				int x = (int)((Gdx.input.getX(i) / (float)Gdx.graphics.getWidth()) * 320);
//				if (!Gdx.input.isTouched(i)) continue;
//				if (x < 32) {
//					set(Keys.DPAD_LEFT, true);
//					left |= true;
//				}
//				if (x > 32 && x < 90) {
//					set(Keys.DPAD_RIGHT, true);
//					right |= true;
//				}
//				if (x > 320 - 64 && x < 320 - 32) {
//					set(Keys.Z, true);
//					z |= true;
//				}
//				if (x > 320 - 32 && x < 320) {
//					set(Keys.X, true);
//					s |= true;
//				}
//			}
//
//			if (left == false) set(Keys.DPAD_LEFT, false);
//			if (right == false) set(Keys.DPAD_RIGHT, false);
//			if (z == false) set(Keys.Z, false);
//			if (s == false) set(Keys.X, false);
//		}
//	}
//
//	public void releaseAllKeys () {
//		for (int i = 0; i < buttons.length; i++) {
//			if (i == UP || i == DOWN) continue;
//			buttons[i] = false;
//		}
//	}
//
//	@Override
//	public boolean keyDown (int keycode) {
//		set(keycode, true);
//		return false;
//	}
//
//	@Override
//	public boolean keyUp (int keycode) {
//		set(keycode, false);
//		return false;
//	}
//
//	@Override
//	public boolean keyTyped (char character) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
//	@Override
//	public boolean touchDown (int x, int y, int pointer, int button) {
//		return false;
//	}
//
//	@Override
//	public boolean touchUp (int x, int y, int pointer, int button) {
//		x = (int)(x / (float)Gdx.graphics.getWidth() * 320);
//		if (x > 160 - 32 && x < 160) {
//			set(Keys.DPAD_UP, !buttons[UP]);
//			if (buttons[UP]) buttons[DOWN] = false;
//		}
//		if (x > 160 && x < 160 + 32) {
//			set(Keys.DPAD_DOWN, !buttons[DOWN]);
//			if (buttons[DOWN]) buttons[UP] = false;
//		}
//		System.out.println("buttons: " + buttons[UP] + ", " + buttons[DOWN]);
//		return false;
//	}
//
//	@Override
//	public boolean touchDragged (int x, int y, int pointer) {
//		return false;
//	}
//
//	@Override
//	public boolean touchMoved (int x, int y) {
//		return false;
//	}
//
//	@Override
//	public boolean scrolled (int amount) {
//		return false;
//	}
}
